package com.tabview.difficultpoint;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;

import com.xudow.app.Config;
import com.xudow.app.XApplication;
import com.xudow.app.util.rest.XRestTemplate;

import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.HashMap;
import java.util.List;

import static com.xudow.app.util.LogUtils.LOGE;
import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 作者：zhouXJ on 2016/4/8 15:41
 */
public class CourseDifficultPointsTask extends AsyncTask<HashMap<String, String>, Void, Void> {

    private static final String TAG = makeLogTag(CourseDifficultPointsTask.class);

    private static final String CLASS_SCHEDULE_URL = Config.BASE_URL + Config.DIFFICULT_POINTS_CONTENT;//"app/study/list_student_class_time";

    public static final int SUCCESS = 0;
    public static final int ERROR = 1;

    private Context context;
    private Handler handler;

    public CourseDifficultPointsTask(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }

    private void sendErrorMessage(String msg) {
        android.os.Message message = new android.os.Message();
        message.what = ERROR;

        if (null != msg) {
            Bundle data = new Bundle();
            data.putString("errMessage", msg);
            message.setData(data);
        }

        handler.sendMessage(message);
    }

    private void sendSuccessMessage(List<CourseDifficultPointContent> svos) {
        android.os.Message message = new android.os.Message();
        message.what = SUCCESS;

        CourseDifficultPointContent[] svoArr = new CourseDifficultPointContent[svos.size()];
        for (int i = 0; i < svos.size(); i++) {
            svoArr[i] = svos.get(i);
        }

        Bundle data = new Bundle();
        data.putSerializable("schedules", svoArr);
        message.setData(data);

        handler.sendMessage(message);
    }


    @Override
    protected Void doInBackground(HashMap<String, String>... hashMaps) {
        XApplication application = (XApplication) context.getApplicationContext();

        XRestTemplate restTemplate = new XRestTemplate(application.getCookieStore(), context);
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        HashMap<String, String> params = hashMaps[0];

        MultiValueMap<String, String> valueMap = new LinkedMultiValueMap<String, String>();

        valueMap.add("studentid", params.get("studentid"));
        valueMap.add("agencyid", params.get("agencyid"));
        valueMap.add("orderinfooaid", params.get("orderinfooaid"));

        LOGE("zxj", "studentid = " + params.get("studentid"));
        LOGE("zxj", "agencyid = " + params.get("agencyid"));
        LOGE("zxj", "orderinfooaid = " + params.get("orderinfooaid"));

        try {
            CourseDifficultPointContentMessage result = restTemplate.postForObject(CLASS_SCHEDULE_URL,
                    valueMap, CourseDifficultPointContentMessage.class);

            if (result != null) {
                if (result.getResult().equals("0")) {

                    sendSuccessMessage(result.getTimes());

                } else if (result.getResult().equals("1")) {
                    sendErrorMessage(result.getErrorMessage());
                }
            } else {
                sendErrorMessage(null);
            }
        } catch (Exception e) {
            LOGE(TAG, e.getMessage());

            sendErrorMessage(e.getMessage());
        }

        return null;
    }
}
